﻿<localControlsMapping:TableMapping x:Class="EDMDesigner.Designer.Controls.Mapping.EntityTableMapping"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wpfToolKit="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:localControls="clr-namespace:EDMDesigner.Designer.Controls"
    xmlns:localControlsMapping="clr-namespace:EDMDesigner.Designer.Controls.Mapping"
    xmlns:localControlsUtil="clr-namespace:EDMDesigner.Designer.Controls.Util"
    xmlns:localBindingConverter="clr-namespace:EDMDesigner.Designer.BindingConverter"
    xmlns:uiBusinessMSL="clr-namespace:EDMDesigner.UIBusiness.MSL;assembly=UIBusiness"
    xmlns:uiBusinessMSLCondition="clr-namespace:EDMDesigner.UIBusiness.MSL.Condition;assembly=UIBusiness"
    xmlns:businessSSDLEntityType="clr-namespace:EDMDesigner.Business.SSDL.EntityType;assembly=Business">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MappingResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <localBindingConverter:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
            <localBindingConverter:ObjectToVisibilityConverter x:Key="ObjectToVisibilityConverter" />
            <localBindingConverter:ConditionOperatorToVisibilityConverter x:Key="ConditionOperatorToVisibilityConverter" />
        </ResourceDictionary>
    </UserControl.Resources>

    <StackPanel Margin="0,5">
        <localControls:ComboBoxEditableWhenFocused x:Name="tablesComboBox" SelectedValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=TableComboBoxValue.ComboSelectedValue}" HorizontalAlignment="Left">
            <ComboBox.ItemsSource>
                <CompositeCollection>
                    <CollectionContainer x:Name="tableNullValues" />
                    <CollectionContainer x:Name="tablesValues" />
                </CompositeCollection>
            </ComboBox.ItemsSource>
            <ComboBox.Resources>
                <DataTemplate DataType="{x:Type localControlsUtil:NullValue}" >
                    <TextBlock Text="{Binding NullText}" FontStyle="Italic" Foreground="Gray" />
                </DataTemplate>
                <DataTemplate DataType="{x:Type businessSSDLEntityType:EntityType}">
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </ComboBox.Resources>
        </localControls:ComboBoxEditableWhenFocused>
        <StackPanel Margin="0,5" Visibility="{Binding ElementName=tablesComboBox, Path=SelectedValue, Converter={StaticResource ObjectToVisibilityConverter}}">
            <StackPanel Margin="5,0,0,5" Orientation="Horizontal">
                <CheckBox Content="TPC" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=TPC}" />
                <CheckBox x:Name="hesCheckBox" Content="HES" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=HES}" Margin="5,0,0,0" />
            </StackPanel>
            <TreeView x:Name="tableMappingTreeView">
                <localControlsMapping:TreeViewItemMapping x:Name="conditionsTreeViewItem" Header="{x:Static localControlsMapping:EntityTableMapping.Conditions}" IsExpanded="True">
                    <localControlsMapping:TreeViewItemMapping x:Name="columnConditionsTreeViewItem" Header="{x:Static localControlsMapping:EntityTableMapping.ColumnConditions}">
                        <StackPanel>
                            <wpfToolKit:DataGrid x:Name="columnConditionsGrid" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=ColumnConditionsMapping}" Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=ColumnConditionMappingVisible, Converter={StaticResource BoolToVisibilityConverter}}" HorizontalAlignment="Stretch" Width="Auto" Margin="-20,0,0,0">
                                <wpfToolKit:DataGrid.Columns>
                                    <wpfToolKit:DataGridTemplateColumn Header="{x:Static localControlsMapping:EntityTableMapping.Column}">
                                        <wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <localControlsMapping:ColumnConditionComboBox Column="{Binding Column}" Table="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=Table, Mode=OneTime}" Deleted="ColumnConditionMapping_ConditionDeleted" />
                                            </DataTemplate>
                                        </wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                    </wpfToolKit:DataGridTemplateColumn>
                                    <wpfToolKit:DataGridTemplateColumn Header="{x:Static localControlsMapping:EntityTableMapping.Operator}">
                                        <wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <localControls:ComboBoxEditableWhenFocused x:Name="columnConditionsOperatorComboBox" ItemsSource="{x:Static uiBusinessMSLCondition:ConditionOperatorList.Instance}" DisplayMemberPath="Text" SelectedValuePath="Value" SelectedValue="{Binding Operator}" HorizontalAlignment="Center" />
                                            </DataTemplate>
                                        </wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                    </wpfToolKit:DataGridTemplateColumn>
                                    <wpfToolKit:DataGridTemplateColumn Header="{x:Static localControlsMapping:EntityTableMapping.Value}">
                                        <wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBox x:Name="columnConditionsValueTextBox" Visibility="{Binding Path=Operator, Converter={StaticResource ConditionOperatorToVisibilityConverter}}" Text="{Binding Value}" />
                                            </DataTemplate>
                                        </wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                    </wpfToolKit:DataGridTemplateColumn>
                                </wpfToolKit:DataGrid.Columns>
                            </wpfToolKit:DataGrid>
                            <Button x:Name="addColumnConditionButton" Content="{x:Static localControlsMapping:EntityTableMapping.AddColumnConditionMapping}" Margin="-20,0,0,0" Foreground="Gray" Background="Transparent" BorderBrush="Transparent" Click="AddColumnConditionButton_Click" />
                        </StackPanel>
                    </localControlsMapping:TreeViewItemMapping>
                    <localControlsMapping:TreeViewItemMapping x:Name="propertyConditionsTreeViewItem" Header="{x:Static localControlsMapping:EntityTableMapping.PropertyConditions}" Visibility="{Binding ElementName=hesCheckBox, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}}">
                        <StackPanel>
                            <wpfToolKit:DataGrid x:Name="propertyConditionsGrid" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=PropertyConditionsMapping}" Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=PropertyConditionMappingVisible, Converter={StaticResource BoolToVisibilityConverter}}">
                                <wpfToolKit:DataGrid.Columns>
                                    <wpfToolKit:DataGridTemplateColumn Header="{x:Static localControlsMapping:EntityTableMapping.Property}">
                                        <wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <localControlsMapping:PropertyConditionComboBox Property="{Binding CSDLProperty}" EntityType="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=EntityType, Mode=OneTime}" Deleted="PropertyConditionComboBox_Deleted" />
                                            </DataTemplate>
                                        </wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                    </wpfToolKit:DataGridTemplateColumn>
                                    <wpfToolKit:DataGridTemplateColumn Header="{x:Static localControlsMapping:EntityTableMapping.Operator}">
                                        <wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <localControls:ComboBoxEditableWhenFocused x:Name="columnConditionsOperatorComboBox" ItemsSource="{x:Static uiBusinessMSLCondition:ConditionOperatorList.Instance}" DisplayMemberPath="Text" SelectedValuePath="Value" SelectedValue="{Binding Operator}" HorizontalAlignment="Center" />
                                            </DataTemplate>
                                        </wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                    </wpfToolKit:DataGridTemplateColumn>
                                    <wpfToolKit:DataGridTemplateColumn Header="{x:Static localControlsMapping:EntityTableMapping.Value}">
                                        <wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBox x:Name="columnConditionsValueTextBox" Visibility="{Binding Path=Operator, Converter={StaticResource ConditionOperatorToVisibilityConverter}}" Text="{Binding Value}" />
                                            </DataTemplate>
                                        </wpfToolKit:DataGridTemplateColumn.CellTemplate>
                                    </wpfToolKit:DataGridTemplateColumn>
                                </wpfToolKit:DataGrid.Columns>
                            </wpfToolKit:DataGrid>
                            <Button x:Name="addPropertyConditionButton" Content="{x:Static localControlsMapping:EntityTableMapping.AddPropertyConditionMapping}"  Click="AddPropertyConditionButton_Click" />
                        </StackPanel>
                    </localControlsMapping:TreeViewItemMapping>
                </localControlsMapping:TreeViewItemMapping>
                <localControlsMapping:TreeViewItemMapping x:Name="propertiesTreeViewItem" Header="{x:Static localControlsMapping:EntityTableMapping.PropertiesMappingHeader}" IsExpanded="True">
                    <StackPanel Margin="-20,0,0,0">
                        <localControlsMapping:EntityPropertiesMapping x:Name="propertiesMappingControl" Mappings="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=PropertiesMapping}" />
                        <localControlsMapping:ComplexPropertiesMapping x:Name="complexPropertiesMappingControl" Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=HasComplexMappings, Converter={StaticResource BoolToVisibilityConverter}}" Mappings="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type localControlsMapping:EntityTableMapping}}, Path=ComplexMappings}"/>
                    </StackPanel>
                </localControlsMapping:TreeViewItemMapping>
            </TreeView>
        </StackPanel>
    </StackPanel>
</localControlsMapping:TableMapping>
